package gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import model.Behandling;
import service.Service;

/**
 * 
 * @author Søren og Daniel Engel
 * 
 */
public class OpretProduktType extends JPanel implements CarlettiPanel {
	private JLabel lblOpretprodukttype;
	private JPanel panel_1;
	private JLabel lblNewLabel;
	private JTextField textField;
	private JLabel lblNewLabel_1;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private final JLabel lblNewLabel_2 = new JLabel(
			"Findes behandlingen ikke i listen?");
	private JButton btnOpretNyBehandling;
	private JList list;
	private MainFrame m;
	private JPanel panel_2;
	private JButton btnSletBehandling;
	private JPanel panel;
	private JPanel panel_3;
	private JButton btnVisProdukttyper;
	private model.ProduktType pt;

	/**
	 * Create the panel.
	 */
	public OpretProduktType(MainFrame m) {
		setLayout(new BorderLayout(0, 0));
		this.m = m;

		panel_1 = new JPanel();
		add(panel_1, BorderLayout.EAST);
		panel_1.setLayout(new BorderLayout(0, 0));

		panel_2 = new JPanel();
		panel_1.add(panel_2);
		panel_2.setLayout(new GridLayout(1, 0, 0, 0));

		btnOpretNyBehandling = new JButton("Opret/rediger behandling");
		btnOpretNyBehandling.setMargin(new Insets(25, 0, 25, 0));
		panel_2.add(btnOpretNyBehandling);

		btnSletBehandling = new JButton("Slet behandling");
		btnSletBehandling
				.addActionListener(new BtnSletBehandlingActionListener());
		panel_2.add(btnSletBehandling);

		btnVisProdukttyper = new JButton("Vis produkttyper");
		btnVisProdukttyper
				.addActionListener(new BtnVisProdukttyperActionListener());
		panel_2.add(btnVisProdukttyper);

		panel_3 = new JPanel();
		panel_1.add(panel_3, BorderLayout.NORTH);
		panel_3.setLayout(new GridLayout(0, 1, 0, 0));

		textField = new JTextField();
		panel_3.add(textField);
		textField.setFont(new Font("Tahoma", Font.PLAIN, 20));
		textField.setColumns(5);

		scrollPane = new JScrollPane();
		panel_3.add(scrollPane);

		list = new JList();
		list.setVisibleRowCount(4);
		list.setFont(new Font("Tahoma", Font.PLAIN, 20));
		scrollPane.setViewportView(list);

		list.setListData(service.Service.getAllBehandlinger().toArray());

		btnNewButton_1 = new JButton("Gem");
		add(btnNewButton_1, BorderLayout.SOUTH);
		btnNewButton_1.setMargin(new Insets(25, 0, 25, 0));

		panel = new JPanel();
		add(panel, BorderLayout.WEST);
		panel.setLayout(new GridLayout(0, 1, 0, 0));

		lblNewLabel = new JLabel("Navn");
		panel.add(lblNewLabel);
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 20));

		lblNewLabel_1 = new JLabel("Behandling");
		panel.add(lblNewLabel_1);
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setFont(new Font("Dialog", Font.BOLD, 20));
		panel.add(lblNewLabel_2);
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_2.setFont(new Font("Dialog", Font.BOLD, 20));
		lblOpretprodukttype = new JLabel("Opret/rediger produkttype");
		add(lblOpretprodukttype, BorderLayout.NORTH);
		lblOpretprodukttype.setHorizontalAlignment(SwingConstants.CENTER);
		lblOpretprodukttype.setFont(new Font("Dialog", Font.BOLD, 30));
		btnNewButton_1.addActionListener(new BtnNewButton_1ActionListener());
		btnOpretNyBehandling
				.addActionListener(new BtnOpretNyBehandlingActionListener());

	}

	private class BtnNewButton_1ActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			if (pt == null && textField.getText().length() > 0
					&& !list.isSelectionEmpty()) {
				Service.opretProduktType(textField.getText(), Service
						.getAllBehandlinger().get(list.getSelectedIndex()));
				m.setVisibelity(m.registrereMellemVare);
			} else if (textField.getText().length() > 0
					&& !list.isSelectionEmpty()) {
				pt.setNavn(textField.getText());
				pt.setBehandlingUD((Behandling) list.getSelectedValue());
				m.setVisibelity(m.registrereMellemVare);
			}
		}
	}

	private class BtnOpretNyBehandlingActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			CRUBehandlingDialog dialog;
			if (list.getSelectedValue() != null) {
				dialog = new CRUBehandlingDialog(m,
						(Behandling) list.getSelectedValue());
			} else {
				dialog = new CRUBehandlingDialog(m, null);
			}
			dialog.setVisible(true);
			list.setListData(service.Service.getAllBehandlinger().toArray());
		}
	}

	private class BtnSletBehandlingActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			if (list.getSelectedValue() != null) {
				service.Service.deleteBehandling((Behandling) list
						.getSelectedValue());
				list.setListData(service.Service.getAllBehandlinger().toArray());
			}
		}
	}

	private class BtnVisProdukttyperActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			m.setVisibelity(m.redigerProduktTyper);
		}
	}

	@Override
	public void update() {
		if (this.pt != null) {
			textField.setText(pt.getNavn());
			list.setSelectedValue(pt.getBehandling(), true);
		} else {
			list.clearSelection();
			textField.setText("");
		}

	}

	public void setProduktType(model.ProduktType pt) {
		this.pt = pt;
	}
}
